package com.hxx.day4.controller;

import com.hxx.day4.domain.Person;
import com.hxx.day4.service.HxxService;
import com.hxx.day4.service.impl.HxxServiceImpl;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class HxxController {

    private HxxService hxxService = new HxxServiceImpl();

    public void save(Scanner scanner){
        Person person = new Person();
        System.out.println("请输入id");
        person.setId(scanner.nextInt());
        System.out.println("请输入姓名");
        person.setName(scanner.next());
        System.out.println("请输入性别");
        person.setSex(scanner.next());
        System.out.println("请输入年龄");
        person.setAge(scanner.nextInt());
        //调用保存方法
        hxxService.save(person);
        System.out.println("保存成功");
    }
    public void update(Scanner scanner){
        System.out.println("请输入一个需要更新的id");
        int id = scanner.nextInt();
        //查找
        Person person1 = new Person();
        person1.setId(id);
        Person person = hxxService.findById(person1);
        //判断
        if(person != null){
            System.out.println("请输入姓名");
            person.setName(scanner.next());
            System.out.println("请输入性别");
            person.setSex(scanner.next());
            System.out.println("请输入年龄");
            person.setAge(scanner.nextInt());
            //调用更新方法
            hxxService.update(person);
            System.out.println("更新成功");
        }else {
            System.out.println("没有此用户");
        }
    }
    public void delete(Scanner scanner){
        System.out.println("请输入一个需要删除的id");
        int id = scanner.nextInt();
        //优化
        Person person = new Person();
        person.setId(id);
        Person person1 = hxxService.findById(person);
        if(person1 != null){
            hxxService.delete(person1);
            System.out.println("删除成功");
        }else {
            System.out.println("没有此用户");
        }
    }
    public void findAll(){
        List list = hxxService.findAll();
        if (list != null){
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }else {
            System.out.println("暂时没有数据");
        }

    }
}
























